import { useRequest } from "../../../utils/request";
import { uploadImage } from "../../../utils/utils";

const WeValidator = require('../../../package/we-validator.min')

Component({
    data: {
        // 正面身份证
        cert_front: '',
        // 手持身份证
        cert_back: '',
        phone: ''
    },
    methods: {
        onLoad() {
            this.request = new useRequest('api/becomehouseowner', {
                hold: true
            }, this)
            this.getCode = new useRequest('api/verifycode', {
                hold: true,
                loading: 'codeLoading'
            }, this)

            this.validatorInstance = new WeValidator({
                rules: {
                    name: {
                        required: true,
                        chinese: true,
                    },
                    cert_no: {
                        required: true,
                        idcard: true
                    },
                    phone: {
                        required: true,
                        mobile: true
                    },
                    sms_code: {
                        required: true,
                    }
                },
                messages: {
                    name: {
                        required: '请输入真实姓名',
                        chinese: '请输入正确的姓名'
                    },
                    cert_no: {
                        required: '请输入身份证',
                        idcard: '请输入正确的身份证'
                    },
                    phone: {
                        required: '请输入手机号',
                        mobile: '请输入正确的手机号'
                    },
                    sms_code: {
                        required: '请输入验证码',
                    }
                },
            })
        },
        uploadImage(e) {
            uploadImage().then(url => {
                this.setData({
                    [e.currentTarget.dataset.type]: url[0]
                })
            })
        },
        startTimeout() {
            this.setData({
                second: 59
            })
            this.timeout = setInterval(() => {
                this.setData({
                    second: this.data.second - 1
                })
                if (this.data.second <= 0) {
                    clearInterval(this.timeout)
                }
            }, 1000)
        },

        getPhoneCode() {
            if (
                WeValidator.checkValue('required', this.data.phone) &&
                WeValidator.checkValue('mobile', this.data.phone, true)
            ) {
                this.getCode.run({ phone: this.data.phone }).then(() => {
                    this.startTimeout()
                })
            }
        },
        submit(e) {
            const value = e.detail.value

            if (this.validatorInstance.checkData(value) && !this.data.loading) {
                if (!WeValidator.checkValue('required', this.data.cert_front)) {
                    wx.showToast({
                        title: '请上传正面身份证'
                    })
                    return false
                }
                if (!WeValidator.checkValue('required', this.data.cert_back)) {
                    wx.showToast({
                        title: '请上传手持身份证'
                    })
                    return false
                }

                this.request.run({
                    ...value,
                    cert_front: this.data.cert_front,
                    cert_back: this.data.cert_back,
                }).then(() => {
                    wx.showToast({
                        title: '提交成功，正在审核...',
                    })
                    wx.navigateBack()
                })
            }
        }
    }
});
